package cn.studyjams.s1.contest.autohello;


import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import cn.studyjams.s1.contest.autohello.adapter.ColorPickerAdapter;
import cn.studyjams.s1.contest.autohello.model.ColorPicker;


/**
 * A simple {@link Fragment} subclass.
 */
public class SetColorFragment extends DialogFragment {

    private final static String TAG = "SetColorFragment";
    private RecyclerView mRecyclerView;
    private List<ColorPicker> mDataset;

    SetColorListener setColorListener;


    public interface SetColorListener {
        void onConfirmSetColor(ColorPicker colorPicker);
    }

    public SetColorFragment() {
        // Required empty public constructor
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            dialog.getWindow()
                    .setLayout((int) (dm.widthPixels * 0.85), ViewGroup.LayoutParams.WRAP_CONTENT);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            setColorListener = (SetColorListener) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString()
                    + "must implement SetColorFragment");
        }
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_set_color, null);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.colors_holder);
        mRecyclerView.setHasFixedSize(true);

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setView(view);

        return builder.create();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(linearLayoutManager);

        mDataset = initDataset();

        RecyclerView.Adapter adapter = new ColorPickerAdapter(mDataset) {
            @Override
            public void onBindViewHolder(final ViewHolder holder, int position) {
                super.onBindViewHolder(holder, position);
                final ColorPicker colorPicker = mDataset.get(position);

                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
//                        Snackbar.make(view, colorPicker.getColorName(), Snackbar.LENGTH_LONG).show();
                        Log.d(TAG, colorPicker.getColorName());
                        SetColorListener listener = (SetColorListener) getActivity();
                        listener.onConfirmSetColor(colorPicker);
                        dismiss();
                    }
                });
            }
        };
        mRecyclerView.setAdapter(adapter);
    }

    private List<ColorPicker> initDataset() {
        List<ColorPicker> dataset = new ArrayList<>();

        String[] names = new String[]{
                getResources().getString(R.string.tomato),
                getResources().getString(R.string.banana),
                getResources().getString(R.string.cucumber),
                getResources().getString(R.string.blue_berry),
                getResources().getString(R.string.grape),
                getResources().getString(R.string.default_color)
        };

        Drawable[] drawables = new Drawable[]{
                ContextCompat.getDrawable(getActivity(), R.drawable.color_tomato_24dp),
                ContextCompat.getDrawable(getActivity(), R.drawable.color_banana_24dp),
                ContextCompat.getDrawable(getActivity(), R.drawable.color_cucumber_24dp),
                ContextCompat.getDrawable(getActivity(), R.drawable.color_blueberry_24dp),
                ContextCompat.getDrawable(getActivity(), R.drawable.color_grape_24dp),
                ContextCompat.getDrawable(getActivity(), R.drawable.color_default_24dp)
        };

        for (int i = 0; i < names.length; i++) {
            ColorPicker colorPicker = new ColorPicker(
                    i,
                    names[i],
                    drawables[i]
            );
            dataset.add(colorPicker);
        }
        return dataset;
    }
}
